<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单跳转测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 100px auto;
            padding: 20px;
            text-align: center;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 5px;
            cursor: pointer;
            margin: 10px;
            font-size: 16px;
        }
        button:hover {
            background: #0056b3;
        }
        .admin-btn {
            background: #ff6b35;
        }
        .admin-btn:hover {
            background: #e55a2b;
        }
        .result {
            margin: 20px 0;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #007bff;
        }
    </style>
</head>
<body>
    <h1>🔧 简单跳转测试</h1>
    
    <div class="result">
        <h3>📊 当前状态</h3>
        <p><strong>用户Token:</strong> <span id="user-token">无</span></p>
        <p><strong>管理员Token:</strong> <span id="admin-token">无</span></p>
        <p><strong>用户类型:</strong> <span id="user-type">无</span></p>
    </div>

    <div>
        <h3>🔗 直接跳转测试</h3>
        <button onclick="testJumpToFrontend()">跳转到前端首页</button>
        <button onclick="testJumpToBackend()" class="admin-btn">跳转到后端管理后台</button>
    </div>

    <div>
        <h3>👨‍💼 管理员登录测试</h3>
        <button onclick="testAdminLogin()" class="admin-btn">测试管理员登录并跳转</button>
    </div>

    <div>
        <h3>🧹 清理操作</h3>
        <button onclick="clearAll()" style="background: #dc3545;">清除所有认证信息</button>
    </div>

    <div id="log" style="margin-top: 30px; text-align: left; background: #f8f9fa; padding: 15px; border-radius: 5px; font-family: monospace; font-size: 12px;">
        <strong>日志:</strong><br>
    </div>

    <script>
        function log(message) {
            const logDiv = document.getElementById('log');
            const time = new Date().toLocaleTimeString();
            logDiv.innerHTML += `[${time}] ${message}<br>`;
            console.log(message);
        }

        function updateStatus() {
            document.getElementById('user-token').textContent = localStorage.getItem('token') || '无';
            document.getElementById('admin-token').textContent = localStorage.getItem('adminToken') || '无';
            document.getElementById('user-type').textContent = localStorage.getItem('userType') || '无';
        }

        function testJumpToFrontend() {
            log('测试跳转到前端首页...');
            window.location.href = 'http://127.0.0.1:5173/home';
        }

        function testJumpToBackend() {
            log('测试跳转到后端管理后台...');
            window.location.href = 'http://127.0.0.1:8000/admin/dashboard';
        }

        async function testAdminLogin() {
            log('开始测试管理员登录...');
            
            try {
                log('发送管理员登录请求...');
                const response = await fetch('http://127.0.0.1:8000/api/admin/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: 'admin@example.com',
                        password: 'admin123'
                    })
                });

                log(`收到响应，状态码: ${response.status}`);
                const data = await response.json();
                log(`响应数据: ${JSON.stringify(data)}`);
                
                if (data.success) {
                    log('管理员登录成功！');
                    localStorage.setItem('adminToken', data.data.token);
                    localStorage.setItem('adminUser', JSON.stringify(data.data.user));
                    localStorage.setItem('userType', 'admin');
                    
                    updateStatus();
                    
                    log('2秒后跳转到后端管理后台...');
                    setTimeout(() => {
                        log('正在跳转到后端管理后台...');
                        window.location.href = 'http://127.0.0.1:8000/admin/dashboard';
                    }, 2000);
                } else {
                    log(`管理员登录失败: ${data.message}`);
                }
            } catch (error) {
                log(`管理员登录错误: ${error.message}`);
            }
        }

        function clearAll() {
            log('清除所有认证信息...');
            localStorage.removeItem('token');
            localStorage.removeItem('adminToken');
            localStorage.removeItem('user');
            localStorage.removeItem('adminUser');
            localStorage.removeItem('userType');
            updateStatus();
            log('已清除所有认证信息');
        }

        // 页面加载时更新状态
        window.onload = function() {
            log('页面加载完成');
            updateStatus();
        };
    </script>
</body>
</html>
